<!-- sidebar: book index -->
<script>
    window.gs_status.current_chapter_uri = null;
    window.gs_status.current_chapter_version = 0;

    function gs_current_pathname() {
        let p = location.pathname;
        if (p.endsWith('/')) {
            p = p + 'index.html';
        }
        return p;
    }

    function gs_set_loading() {
        document.getElementById('gsi-content').innerHTML = `<div class="block w-5 h-5">
<svg class="animate-spin ml-1 mr-3 h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg></div>`;
        window.scrollTo({ top: 0 });
    }

    function gs_find_parent_node(node, tagName) {
        tagName = tagName.toLowerCase();
        while (node.tagName.toLowerCase() !== tagName) {
            node = node.parentNode;
        }
        return node;
    }

    function gs_set_current_chapter_uri(uri) {
        const old_uri = window.gs_status.current_chapter_uri;
        console.log(`set chapter: ${old_uri} => ${uri}`);
        if (old_uri) {
            // dynamic load part of page:
            document.querySelector(`#gsi-index a[href="{{ site.rootPath }}${old_uri}"]`).classList.remove('gsc-active');
        }
        const a = document.querySelector(`#gsi-index a[href="{{ site.rootPath }}${uri}"]`);
        a.classList.add('gsc-active');
        // set new current uri:
        window.gs_status.current_chapter_uri = uri;
        // path from root to active item should be open:
        const root = document.querySelector(`#gsi-index`);
        // find active '<div>':
        let node = gs_find_parent_node(a, 'div');
        while (node !== root) {
            if (node.tagName.toLowerCase() === 'div') {
                let span_closed = node.querySelector(':scope > a span.gsc-index-item-closed');
                if (span_closed) {
                    gs_index_item_collapse({ target: span_closed }, 'closed', 'open');
                }
            }
            node = node.parentNode;
        }
    }

    function gs_load_chapter(uri, shouldPushState) {
        if (uri === window.gs_status.current_chapter_uri) {
            return;
        }
        gs_set_current_chapter_uri(uri);
        if (shouldPushState) {
            console.log(`push state: ${uri}`);
            history.pushState({}, '', `{{ site.rootPath }}${uri}`);
        }
        console.log(`try load new chapter: ${uri}`);
        if (window.gs_status.show_offcanvas) {
            gitsite.hideOffcanvas();
        }
        gs_set_loading();
        window.gs_status.current_chapter_version++;
        const version = window.gs_status.current_chapter_version;
        gs_load_chapter_content(uri).then((html) => {
            if (version === window.gs_status.current_chapter_version) {
                console.log(`loaded ok: ${uri}`);
                gitsite.setInnerHTML(document.querySelector('#gsi-content'), html);
                document.title = document.querySelector('#gsi-chapter-title h1').innerHTML + ' - ' + window.gs_status.title_suffix;
                gitsite.triggerContentChangedListener();
            }
        }).catch(err => {
            console.error(err);
        });
    }

    async function gs_load_chapter_content(uri) {
        // load /books/<name>/<chapter>/content.html:
        let chapter_uri = uri.substring(0, uri.length - 'index.html'.length) + 'content.html';
        let resp = await fetch(`{{ site.rootPath }}${chapter_uri}`);
        if (resp.status === 200) {
            return await resp.text();
        }
    }

    // set open/closed:
    function gs_index_item_collapse(e, fromState, toState) {
        e.stopPropagation && e.stopPropagation();
        // div.gsc-index-item > a > span.gsc-index-item-open-or-closed:click 
        let node = gs_find_parent_node(e.target, 'div');
        node.classList.remove(`gsc-index-item-${fromState}`);
        node.classList.add(`gsc-index-item-${toState}`);
        return false;
    }
</script>
<!--
{% set icon_closed = '<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M8.25 4.5l7.5 7.5-7.5 7.5" /></svg>' %}
{% set icon_open = '<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-4 h-4"><path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5" /></svg>' %}
{% macro index_item(node) %}
<div class="gsc-index-item gsc-index-item-closed">
    <a class="truncate" href="{{ site.rootPath }}/books/{{ node.uri }}/index.html" onclick="gs_load_chapter('/books/{{ node.uri }}/index.html',true);return false;">
        {% if site.books and site.books.indexMarker %}
        <span class="gsc-index-item-marker">{{ node.marker }}.</span>
        {% endif %}
        <span class="gsc-index-item-title">{{ node.title }}</span>
        {% if node.children.length > 0 %}
        <span class="gsc-index-item-action gsc-index-item-closed" onclick="return gs_index_item_collapse(event, 'closed', 'open')">
            {{ icon_closed | safe }}
        </span>
        <span class="gsc-index-item-action gsc-index-item-open" onclick="return gs_index_item_collapse(event, 'open', 'closed')">
            {{ icon_open | safe }}
        </span>
        {% endif %}
    </a>
    {% if node.children.length > 0 %}
    <div class="gsc-index-item-children">
        {% for item in node.children %}
        {{ index_item(item) }}
        {% endfor %}
    </div>
    {% endif %}
</div>
{% endmacro %}
-->
<div id="gsi-index">
    <h3>{{ book.title }}</h3>
    {% for item in book.children %}
    {{ index_item(item) }}
    {% endfor %}
</div>
<script>
    gs_set_current_chapter_uri(gs_current_pathname().substring('{{ site.rootPath }}'.length));
    window.onpopstate = function (e) {
        console.log(`pop state: ${gs_current_pathname()}`);
        gs_load_chapter(gs_current_pathname().substring('{{ site.rootPath }}'.length));
    }
</script>
<!--// sidebar: book index -->